//
//  RTEmotionKeyboard.swift
//  Weibo
//
//  Created by Brisinga on 16/3/14.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

class RTEmotionKeyboard: UIView {
    
  
    private let identifier: String = "pageCell"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 加载内部组件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 加载内部组件
    private func setupUI() {
    
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        // 设置toolbar
        addSubview(toolbar)
        
        toolbar.snp_makeConstraints { (make) -> Void in
            
            make.bottom.leading.trailing.equalTo(self)
        }
        
        
        
        // 设置collectionView
        addSubview(collectionView)
        
        collectionView.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
        }
        
        // 设置pageControl
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.bottom.equalTo(collectionView)
        }
        
    }
    
    
    // 设置pageControl
    private func setupPageControl(indexPath: NSIndexPath) {
    
        pageControl.numberOfPages = RTEmotionTools.sharedEmotionTools.allEmoticons[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
        
        pageControl.hidden = indexPath.section == 0
    }
    
    
    // 懒加载
    
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        pageControl.userInteractionEnabled = false
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKeyPath: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKeyPath: "_pageImage")
        
        return pageControl
    }()
    
    private lazy var toolbar: RTEmotionToolBar = {
    
        let toolbar = RTEmotionToolBar(frame: CGRectZero)
        
        toolbar.delegate = self
        
        
        return toolbar
    }()
    
    
    private lazy var collectionView: RTEmotionCollectionView = {
    
        let collectionView = RTEmotionCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.registerClass(RTEmotionPageCell.self, forCellWithReuseIdentifier: self.identifier)
        
        return collectionView
    }()
    
}

extension RTEmotionKeyboard: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return RTEmotionTools.sharedEmotionTools.allEmoticons.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return RTEmotionTools.sharedEmotionTools.allEmoticons[section].count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! RTEmotionPageCell
        
//        cell.backgroundColor = RandomColor()
        
        cell.indexPath = indexPath
        
        return cell
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let cells = collectionView.visibleCells()
        
        if cells.count == 2 {
        
            let firstCell = cells.first
            let lastCell = cells.last

            let firstResult = abs((firstCell?.frame.origin.x)! - collectionView.contentOffset.x)
            
            let lastResult = abs((lastCell?.frame.origin.x)! - collectionView.contentOffset.x)
            
            let cell = firstResult < lastResult ? firstCell : lastCell
            
            // 取到cell对应的indexPath
            let indexPath = collectionView.indexPathForCell(cell!)!
            
            toolbar.selectButtonWithSection(indexPath.section)
            
            setupPageControl(indexPath)

        
        }
    }

}


extension RTEmotionKeyboard: RTEmotionToolBarDelegate {

    func emotionToolBar(toolBar: RTEmotionToolBar, type: RTEmotionToolBarType) {
        
        print(type)
        
        let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
        
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        
        setupPageControl(indexPath)
        
    }
}
